package com.uatair.common.mqtt.util;

import com.google.protobuf.Message;
import com.google.protobuf.util.JsonFormat;
import com.uatair.common.kafka.domain.protobuf.FlightProto;
import com.uatair.common.tcp.util.TianjinTcp;

import java.io.IOException;

public class ProtobufUtil {

    private static final JsonFormat.Printer printer = JsonFormat.printer().includingDefaultValueFields();
    private static final JsonFormat.Parser parser = JsonFormat.parser().ignoringUnknownFields();

    public static String toJson(FlightProto.FlightRecord record) throws IOException {
        return printer.print(record);
    }

    public static String toTianJinJson(TianjinTcp.Message record) throws IOException {
        return printer.print(record);
    }

    public static String toDetailJson(FlightProto.FlightRecordDetail detail) throws IOException {
        return printer.print(detail);
    }

    public static FlightProto.FlightRecord fromJson(String json) throws IOException {
        FlightProto.FlightRecord.Builder builder = FlightProto.FlightRecord.newBuilder();
        parser.merge(json, builder);
        return builder.build();
    }

    public static FlightProto.FlightRecord parseFrom(byte[] data) throws IOException {
        return FlightProto.FlightRecord.parseFrom(data);
    }
}